package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.system.domain.SysDictType;
import org.dromara.system.service.impl.SysDictTypeServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseService {

  private final SysDictTypeServiceImpl dictTypeService;

  /**
   * 查询字典类型列表
   */
  @SaCheckPermission("system:dict:list")
  @GetMapping("/list")
  public TableDataInfo<SysDictType> list(SysDictType dictType, PageQuery pageQuery) {

    return dictTypeService.selectPageDictTypeList(dictType, pageQuery);
  }

  /**
   * 导出字典类型列表
   */
  @Log(title = "字典类型", businessType = BusinessType.EXPORT)
  @SaCheckPermission("system:dict:export")
  @PostMapping("/export")
  public void export(SysDictType dictType, HttpServletResponse response) {
    List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
    ExcelUtil.exportExcel(list, "字典类型", SysDictType.class, response);
  }

  /**
   * 查询字典类型详细
   *
   * @param dictId 字典ID
   */
  @SaCheckPermission("system:dict:query")
  @GetMapping(value = "/{dictId}")
  public R<SysDictType> getInfo(@PathVariable Long dictId) {
//        return R.ok(dictTypeService.selectDictTypeById(dictId));
    return R.ok(dictTypeService.selectDictTypeById(dictId));

  }

  /**
   * 新增字典类型
   */
  @SaCheckPermission("system:dict:add")
  @Log(title = "字典类型", businessType = BusinessType.INSERT)
  @PostMapping
  public R<Void> add(@Validated @RequestBody SysDictType dict) {
    if (!dictTypeService.checkDictTypeUnique(dict)) {
      return R.fail("新增字典'" + dict.getName() + "'失败，字典类型已存在");
    }
    dictTypeService.insertDictType(dict);
    return R.ok();
  }

  /**
   * 修改字典类型
   */
  @SaCheckPermission("system:dict:edit")
  @Log(title = "字典类型", businessType = BusinessType.UPDATE)
  @PutMapping
  public R<Void> edit(@Validated @RequestBody SysDictType dict) {
    if (!dictTypeService.checkDictTypeUnique(dict)) {
      return R.fail("修改字典'" + dict.getName() + "'失败，字典类型已存在");
    }
    dictTypeService.updateDictType(dict);
    return R.ok();
  }

  /**
   * 删除字典类型
   *
   * @param dictIds 字典ID串
   */
  @SaCheckPermission("system:dict:remove")
  @Log(title = "字典类型", businessType = BusinessType.DELETE)
  @DeleteMapping("/{dictIds}")
  public R<Void> remove(@PathVariable Long[] dictIds) {
    dictTypeService.deleteDictTypeByIds(dictIds);
    return R.ok();
  }

  /**
   * 刷新字典缓存
   */
  @SaCheckPermission("system:dict:remove")
  @Log(title = "字典类型", businessType = BusinessType.CLEAN)
  @DeleteMapping("/refreshCache")
  public R<Void> refreshCache() {
    dictTypeService.resetDictCache();
    return R.ok();
  }

  /**
   * 获取字典选择框列表
   */
  @GetMapping("/optionselect")
  public R<List<SysDictType>> optionselect() {
    List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
    return R.ok(dictTypes);
  }
}
